package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.ConversationListInfo;
import com.beiing.tianshuai.tianshuai.util.TimeUtils;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.text.MessageFormat;
import java.text.ParseException;

/**
 * Created by Jinni on 2017/9/6 0006.
 */

public class SearchContactListAdapter extends BaseRecyclerViewAdapter<ConversationListInfo> {

    private Context mContext;
    private OnItemClickListener mListener;

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public SearchContactListAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size();
        } else {
            return 0;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = View.inflate(mContext, R.layout.item_search_contact, null);
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ConversationListInfo info = mList.get(position);
        // 设置用户最新消息内容
        ((ViewHolder) holder).mMessage.setText(info.getLastMessage());
        ((ViewHolder) holder).mUserName.setText(info.getUid());
        ((ViewHolder) holder).mItem.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mListener.onItemClickListener(info.getUid(), info.getChatType());
                    }
                }
        );
        try {
            if (TimeUtils.isToday(TimeUtils.longToString(
                    Long.parseLong(String.valueOf(info.getTime())),
                    "yyyy-MM-dd HH:mm:ss"))) {
                ((ViewHolder) holder).mLastTime.setText(
                        MessageFormat.format(
                                "{0}",
                                TimeUtils.longToString(
                                        Long.parseLong(String.valueOf(info.getTime())),
                                        "HH:mm")
                        )
                );
            } else {
                ((ViewHolder) holder).mLastTime.setText(
                        MessageFormat.format(
                                "{0}",
                                TimeUtils.longToString(
                                        Long.parseLong(String.valueOf(info.getTime())),
                                        "MM月dd日")
                        )
                );
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (info.getUnreadMessageCount() > 0) {
            ((ViewHolder) holder).mUnreadNum.setVisibility(View.VISIBLE);
            ((ViewHolder) holder).mUnreadNum.setText(
                    info.getUnreadMessageCount() > 99 ?
                            "99+" : String.valueOf(info.getUnreadMessageCount())
            );
        } else {
            ((ViewHolder) holder).mUnreadNum.setVisibility(View.GONE);
        }
        String avatar = info.getAvatar();
        String nickname = info.getNickname();
        RequestOptions requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .override(350, 350);
        Glide.with(mContext.getApplicationContext())
                .load(TextUtils.isEmpty(avatar) ? R.mipmap.default_avatar : avatar)
                .apply(requestOptions)
                .into(((ViewHolder) holder).mUserAvatar);
        ((ViewHolder) holder).mUserName.setText(nickname);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public CircleImageView          mUserAvatar;
        public TextView                 mUserName;
        public TextView                 mLastTime;
        public TextView                 mMessage;
        public TextView                 mUnreadNum;
        public RelativeLayout           mItem;

        public ViewHolder(View convertView) {
            super(convertView);
            mItem = convertView.findViewById(R.id.user_item);
            mUserAvatar = convertView.findViewById(R.id.civ_user_avatar);
            mUnreadNum = convertView.findViewById(R.id.unread_count_point);
            mUserName = convertView.findViewById(R.id.tv_user_nickname);
            mMessage = convertView.findViewById(R.id.last_message_content);
            mLastTime = convertView.findViewById(R.id.tv_last_time);
        }
    }

    public interface OnItemClickListener {
        void onItemClickListener(String uid, int chatType);
    }

    public ConversationListInfo getListinfo(int position) {
        return mList.get(position);
    }
}
